package supertuxwar;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

/**
 * The example Alien actor running around. This is responsible for rendering
 * specifics for this game character. Everything physics related is handled
 * up in Actor.
 * 
 * @author kevin
 */
public class SuperTux extends Actor {
	private SpriteSheet smallTuxIdle;
	private SpriteSheet smallTuxJump;
	private SpriteSheet smallTuxKick;
	private SpriteSheet smallTuxSkid;
	private SpriteSheet smallTuxStand;
	private SpriteSheet smallTuxWalk;
	private SpriteSheet smallTuxDeath;
	
	/** The current frame of animation */
	private int frame;
	/** The timer controlling frame change */
	private int frameTimer = 0;
	/** The interval between animation frame change in milliseconds */
	private int frameInterval = 100;
	
	private float deathY;
	private int deathTimer;
	
	/**
	 * Create a new Alien actor read to add to the world
	 * 
	 * @param x The x position of the alien
	 * @param y The y position of the alien
	 * @param mass The mass of the alien
	 * @param size The size of the alien (collision size)
	 * @throws SlickException Indicates a failure to load resources for this alien
	 */
	public SuperTux(float x, float y, float mass, float size, boolean facingRight) throws SlickException {
		super(x, y, mass, size, facingRight);
	
		smallTuxIdle = new SpriteSheet("res/smalltux-idle-right.png", 49, 48);
		smallTuxJump = new SpriteSheet("res/smalltux-jump-right.png", 49, 48);
		smallTuxKick = new SpriteSheet("res/smalltux-kick-right.png", 49, 48);
		smallTuxSkid = new SpriteSheet("res/smalltux-skid-right.png", 49, 48);
		smallTuxStand = new SpriteSheet("res/smalltux-stand-right.png", 49, 48);
		smallTuxWalk = new SpriteSheet("res/smalltux-walk-right.png", 49, 48);
		smallTuxDeath = new SpriteSheet("res/smalltux-death.png", 57, 43);
	}
	
	public String getName() {
		return "SuperTux";
	}

	/**
	 * @see org.newdawn.penguin.Actor#render(org.newdawn.slick.Graphics)
	 */
	public void render(Graphics g) {
		// work out which animation we're showing based 
		// on the state of the actor
		SpriteSheet sheet = smallTuxStand;
		int sx = 0;
		int sy = 0;
		
		if(death()) {
			int f = (frame % 2) + 1;
			sheet = smallTuxDeath;
			sx = f % 2;
			sy = 0;
		} else if (jumping()) {
			sheet = smallTuxJump;
			sx = 0;
			sy = 0;
		} else if (falling()) {
			sheet = smallTuxJump;
			sx = 0;
			sy = 0;
		} else if (moving() && onGround()) {
			int f = (frame % 8) + 1;
			sheet = smallTuxWalk;
			sx = f % 8;
			sy = 0;
 		} else if (onGround()) {
			sheet = smallTuxStand;
			sx = 0;
			sy = 0;
		}
		
		// get the approprite sprite 
		Image image = sheet.getSprite(sx,sy);
	
		// if we're facing the other direction, flip the sprite over
		if (!facingRight()) {
			image = image.getFlippedCopy(true, false);
		}
		
		// 这里要按照图像的中心绘制，而getX(),getY()实际上是hitbox的中心，需要修正
		if(!death()) {
			image.drawCentered(getX()-4, getY()-8);
		} else {
			// 角色已经死亡，起始位置为getX()，
			image.drawCentered(getX(), deathY);
		}
		
		g.drawString("x=" + getX() + " y=" + getY(), getX(), getY()-40);
	}
	
	/**
	 * @see org.newdawn.penguin.Actor#preUpdate(int)
	 */
	public void preUpdate(int delta) {
		super.preUpdate(delta);
		
		frameTimer -= delta;
		while (frameTimer < 0) {
			frame++;
			frameTimer += frameInterval;
		}
		
		if(death()) {
			deathTimer += delta;

			// TODO: 进一步优化
			deathY = getY() - 500 * deathTimer/1000 + 0.5f*500*deathTimer*deathTimer/1000000;
		}
	}

}
